<template>
  <el-card>
    <el-row v-if="isAdmin" :gutter="20" class="header">
      <el-col :span="7">
        <el-input v-model="queryForm.name" placeholder="请输入用户昵称..." clearable />
      </el-col>
      <el-button type="primary" @click="initUserList">搜索</el-button>
    </el-row>

    <el-table :data="tableData" stripe style="width: 100%;text-align: center">
      <el-table-column prop="id" label="ID" width="80" />

      <el-table-column prop="name" label="用户昵称" />
      <el-table-column prop="identity" label="身份" width="200">
        <template v-slot="scope">
          <el-tag v-if="scope.row.identity == 0">学生</el-tag>
          <el-tag v-if="scope.row.identity == 1" type="danger">教师</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="studentId" label="账号" />
      <el-table-column prop="password" label="密码" />
      <el-table-column prop="createTime" label="注册日期" width="200" />
      
      <el-table-column v-if="isAdmin" label="操作" width="200">
        <template v-slot="scope">
          <el-button type="primary" size="small" icon="el-icon-edit-outline" @click="update(scope.row)">修改</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="deleted(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="queryForm.pageNum"
      :page-sizes="[10, 20, 30, 40,50]"
      :page-size="queryForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      title="修改信息"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
    >
      <div>
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
          <el-form-item label="学号" prop="studentId">
            <el-input v-model="ruleForm.studentId" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { getUserList, updateUser, deletedUser } from '@/api/table'
export default {
  data() {
    return {
      queryForm: {
        pageSize: 10,
        pageNum: 1,
        name: ''
      },
      tableData: [],
      total: 0,
      centerDialogVisible: false,
      ruleForm: {},
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        studentId: [
          { required: true, message: '请输入学号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]

      },
      userInfo: {}
    }
  },
  created() {
    this.initUserList()
  },
  computed: {
    isAdmin() {
      const roles = this.$store.getters.roles
      console.log(roles,'roles');
      
      return Array.isArray(roles) && roles.includes('admin')
    },
    currentUser() {
      return this.$store.getters.user || {}
    }
  },
  methods: {
    initUserList() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      console.log(userInfo,'userInfo');
       
      if (userInfo && userInfo.studentId === 'admin') {
        // 管理员可以查看所有用户
        getUserList(this.queryForm).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      } else {
        // 非管理员只能看到自己的信息
        if (userInfo) {
          this.tableData = [userInfo]
          this.total = 1
        }
      }
      // if (this.isAdmin) {
      //   // 管理员可以查看所有用户
      //   getUserList(this.queryForm).then(res => {
      //     console.log(res,'res');
          
      //     if (res.code == 200) {
      //       this.tableData = res.data.records
      //       this.total = res.data.total
      //     }
      //   })
      // } else {
      //     // 非管理员只能看到自己的信息
      //     const currentUser = this.currentUser
      //   if (currentUser && Object.keys(currentUser).length > 0) {
      //     this.tableData = [currentUser]
      //     this.total = 1
      //   }
      // }
    },
    handleSizeChange(e) {
      console.log(e)
      this.queryForm.pageSize = e
      this.initUserList()
    },
    handleCurrentChange(e) {
      this.queryForm.pageNum = e
      this.initUserList()
    },
    update(item) {
      this.centerDialogVisible = true
      this.ruleForm = item
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateUser(this.ruleForm).then(res => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            this.centerDialogVisible = false
            this.initUserList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    deleted(item) {
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletedUser({ id: item.id }).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initUserList()
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
